package com.smart.util;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.smart.model.PCAuthority;


public class SAXParserHandler extends DefaultHandler {
	private List<PCAuthority> pcAuthorityList=new ArrayList<PCAuthority>();
	private PCAuthority pcAuthority=null;
	private String value=null;
    public List<PCAuthority> getPcAuthorityList() {
		return pcAuthorityList;
	}
/**
  * 用来遍历xml文件的开始标签 
  */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if(qName.equals("authority"))
		{
			pcAuthority=new PCAuthority();
		}
	}
	
	/**
	  * 用来遍历xml文件的结束标签 
	  */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
		if(qName.equals("authority"))
		{
			pcAuthorityList.add(pcAuthority);
			pcAuthority=null;
		}
		else if(qName.equals("url"))
		{
			pcAuthority.setUrl(value);
		}
		else if(qName.equals("role"))
		{
			List<String> role=new ArrayList<String>();
			String[] arry=value.split(",");
			for(String s:arry)
			{
				role.add(s);
			}
			pcAuthority.setRole(role);
		}

	}
	/**
	  * 用来标识解析开始
	  */
	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
	}
	
	/**
	  * 用来标识解析结束 
	  */
	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		value=new String(ch,start,length);
	}	
	
}
